package com.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

/**
 * @author Macrydy
 * @date 2023/4/28
 */
public class WriteDemo {
    public static void main(String[] args) throws Exception {
        //03版
        writeExcel03();
        //07版
        writeExcel07();
        //07大数据版
        writeExcel07s();

    }


    /**
     * 03版
     */
    public static void writeExcel03() throws Exception {
        //创建工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建sheet页
        Sheet sheet = workbook.createSheet("sheet1");
        //创建行
        //Row row = sheet.createRow(0);
        //根据行创建列
        //Cell cell = row.createCell(0);

        for (int i = 0; i < 10; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 5; j++) {
                row.createCell(j).setCellValue(i+","+j);
            }
        }

        //生成表
        FileOutputStream fileOutputStream = new FileOutputStream("excel03.xls");
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();
        System.out.println("生成完毕！");
    }

    /**
     * 07版
     * 注意：表格后缀为xlsx
     * @throws Exception
     */
    public static void writeExcel07() throws Exception {
        //创建工作簿
        Workbook workbook = new XSSFWorkbook();
        //创建sheet页
        Sheet sheet = workbook.createSheet("sheet1");
        //创建行
        //Row row = sheet.createRow(0);
        //根据行创建列
        //Cell cell = row.createCell(0);
        long start = System.currentTimeMillis()/1000;

        for (int i = 0; i < 100000; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 5; j++) {
                row.createCell(j).setCellValue(i+","+j);
            }
        }

        //生成表
        FileOutputStream fileOutputStream = new FileOutputStream("excel07.xlsx");
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();

        long end = System.currentTimeMillis()/1000;
        System.out.println("生成完毕！"+"总用时："+(end-start));
    }

    /**
     * 07大数据加强版
     * 注意：表格后缀为xlsx
     * @throws Exception
     */
    public static void writeExcel07s() throws Exception {
        //创建工作簿
        Workbook workbook = new SXSSFWorkbook();
        //创建sheet页
        Sheet sheet = workbook.createSheet("sheet1");
        //创建行
        //Row row = sheet.createRow(0);
        //根据行创建列
        //Cell cell = row.createCell(0);
        long start = System.currentTimeMillis()/1000;

        for (int i = 0; i < 100000; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 5; j++) {
                row.createCell(j).setCellValue(i+","+j);
            }
        }

        //生成表
        FileOutputStream fileOutputStream = new FileOutputStream("excel07s.xlsx");
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();

        long end = System.currentTimeMillis()/1000;
        System.out.println("生成完毕！"+"总用时："+(end-start));
    }
}
